<!-- 角色功能 -->
<template>
  <van-form ref="form">
    <!-- 角色 -->
    <div class="more">
      <p class="bb">
        <img src="@/assets/img/gm/gy.png" alt="" />
        角色
      </p>
      <van-field
        v-model="form.name"
        name="name"
        label="角色"
        placeholder="请选择角色"
        @click="openPicket"
        readonly
        :rules="[{ required: true, message: '请选择角色' }]"
      />
      <van-popup v-model="showPicker" round position="bottom">
        <van-picker
          show-toolbar
          :columns="avatarList"
          @cancel="showPicker = false"
          @confirm="onConfirm"
        >
          <template #option="option">
            <div style="display: flex; align-items: center">
              <div>{{ option.id }}---</div>
              <div>{{ option.name }}</div>
            </div>
          </template>
        </van-picker>
      </van-popup>
      <!-- <button type="button" class="btn btn-01 btn-block" @click="avatarFun()">
        添加
      </button> -->
      <div class="btn-g">
        <button type="button" class="btn btn-02" @click="avatarFun(1)">
          删除
        </button>
        <button type="button" class="btn btn-02" @click="avatarFun(2)">
          添加
        </button>
      </div>
    </div>
    <!-- 技能等级 -->
    <div class="more">
      <p class="bb">
        <img src="@/assets/img/gm/1.png" alt="" />
        技能等级
      </p>
      <van-field
        v-model="form.jn"
        name="jn"
        label="技能"
        placeholder="请选择技能"
        @click="showPickerJn = true"
        :rules="[{ required: true, message: '请选择技能' }]"
      />
      <van-popup v-model="showPickerJn" round position="bottom">
        <van-picker
          show-toolbar
          :columns="jnList"
          @cancel="showPicker = false"
          @confirm="onConfirmJn"
        >
          <template #option="option">
            <div style="display: flex; align-items: center">
              <div>{{ option.name }}</div>
            </div>
          </template>
        </van-picker>
      </van-popup>
      <van-field name="level" label="等级">
        <template #input>
          <div class="qm-slider-box">
            <div class="qm-slider">
              <van-slider min="1" max="10" v-model="form.level" />
            </div>
            <div class="qm-title">{{ form.level }}</div>
          </div>
        </template>
      </van-field>
      <button type="button" class="btn btn-01 btn-block" @click="setLevel()">
        设置
      </button>
    </div>
    <!-- 冒险等级 -->
    <div class="more">
      <p class="bb">
        <img src="@/assets/img/gm/2.png" alt="" />
        冒险等级
      </p>
      <van-field name="mxlevel" label="冒险等级">
        <template #input>
          <div class="qm-slider-box">
            <div class="qm-slider">
              <van-slider min="1" max="60" v-model="form.mxlevel" />
            </div>
            <div class="qm-title">{{ form.mxlevel }}</div>
          </div>
        </template>
      </van-field>
      <button
        type="button"
        class="btn btn-01 btn-block"
        @click="setOtherLevel(1)"
      >
        设置
      </button>
    </div>
    <!-- 突破等级 -->
    <div class="more">
      <p class="bb">
        <img src="@/assets/img/gm/3.png" alt="" />
        角色突破等级
      </p>
      <van-field name="tplevel" label="突破等级">
        <template #input>
          <div class="qm-slider-box">
            <div class="qm-slider">
              <van-slider min="1" max="6" v-model="form.tplevel" />
            </div>
            <div class="qm-title">{{ form.tplevel }}</div>
          </div>
        </template>
      </van-field>
      <button
        type="button"
        class="btn btn-01 btn-block"
        @click="setOtherLevel(2)"
      >
        设置
      </button>
    </div>
    <!-- 角色等级 -->
    <div class="more">
      <p class="bb">
        <img src="@/assets/img/gm/6.png" alt="" />
        角色等级
      </p>
      <van-field name="jslevel" label="角色等级">
        <template #input>
          <div class="qm-slider-box">
            <div class="qm-slider">
              <van-slider min="1" max="90" v-model="form.jslevel" />
            </div>
            <div class="qm-title">{{ form.jslevel }}</div>
          </div>
        </template>
      </van-field>
      <button
        type="button"
        class="btn btn-01 btn-block"
        @click="setOtherLevel(3)"
      >
        设置
      </button>
    </div>
  </van-form>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import AvatarApi from '@/api/gms/avatar'
export default {
  //import引入的组件需要注入到对象中才能使用
  name: 'avatar-gm',
  components: {},
  data() {
    //这里存放数据
    return {
      //所有物品数据
      avatarList: [],
      showPicker: false,
      showPickerJn: false,
      jnList: [
        { id: '1', name: '普通攻击' },
        { id: '2', name: '元素战技' },
        { id: '3', name: '元素爆发' },
        { id: 'all', name: '所有技能' }
      ],
      form: {
        jn: null,
        name: null,
        level: 1,
        mxlevel: 1,
        tplevel: 1,
        jslevel: 1
      },
      sendForm: {
        id: null,
        jnid: null
      }
    }
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    //打开选择器
    openPicket() {
      this.showPicker = true
    },
    // 选中角色
    onConfirm(value) {
      this.form.name = value.name
      this.sendForm.id = value.id
      this.showPicker = false
    },
    //选中技能
    onConfirmJn(value) {
      this.form.jn = value.name
      this.sendForm.jnid = value.id
      this.showPickerJn = false
    },
    //角色或删除添加
    async avatarFun(type) {
      // 校验
      await this.$refs.form.validate('name')
      const res = await AvatarApi.addOrDelAvatar(type, { id: this.sendForm.id })
      if (res && res.code == 200) {
        this.$toast.success('success')
      }
    },
    //设置技能等级
    async setLevel() {
      // 校验
      await this.$refs.form.validate('jn')
      const res = await AvatarApi.setLevel(this.sendForm.jnid, {
        level: this.form.level
      })
      if (res && res.code == 200) {
        this.$toast.success('success')
      }
    },
    //设置其他等级 type[1：冒险等级 2：角色突破等级 3：角色等级]
    async setOtherLevel(type) {
      let param = { level: '0' }
      if (type == 1) {
        param.level = this.form.mxlevel
      } else if (type == 2) {
        param.level = this.form.tplevel
      } else if (type == 3) {
        param.level = this.form.jslevel
      }
      const res = await AvatarApi.setOtherLevel(type, param)
      if (res && res.code == 200) {
        this.$toast.success('success')
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  async created() {
    const res = await AvatarApi.getAllAvatar()
    if (res && res.code == 200) {
      this.avatarList = res.data
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeUnmount() {}, //生命周期 - 销毁之前
  unmounted() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped lang="less">
.van-cell {
  padding: 20px;
  margin-bottom: 20px;
  &::after {
    left: 0;
    right: 0;
  }
}

.qm-slider-box {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  .qm-slider {
    width: 80%;
    height: 100%;
    display: flex;
    justify-content: center;
    margin-right: 20px;
  }
  .qm-title {
    width: 25px;
    height: 25px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 5px;
    // color: #ddd;
    background: #ddd;
  }
}
.more {
  margin: 0 10px;
  padding: 0px 0px 15px 0px;
  background-color: #fff;
  border-radius: @radius;
  margin-bottom: 20px;
  p {
    font-size: 16px;
    padding: 10px 15px;
    margin: 0;
    border-bottom: 1px solid #00000014;
    display: flex;
    align-items: center;
    img {
      width: 30px;
      height: 30px;
      margin-right: 5px;
    }
  }
}
.btn {
  padding: 6px 12px;
  font-size: 14px;
  text-align: center;
  white-space: nowrap;
  border: 1px solid transparent;
  border-radius: 4px;
  font-weight: 400;
  vertical-align: middle;
  cursor: pointer;
  user-select: none;
}

.btn-block {
  display: block;
}
.btn-01 {
  width: 90%;
  margin-left: 5%;
  height: 40px;
  color: #fff;
  font-weight: 700;
  background-color: #096;
  border: 1px dashed #ff0;
}

.btn-02 {
  width: 40%;
  height: 40px;
  color: #fff;
  font-weight: 700;
  background-color: #096;
  border: 1px dashed #ff0;
}

.btn-g {
  display: flex;
  justify-content: space-evenly;
}
</style>
